// 引入request请求函数
import { request } from '../../request/request'
import { showToast } from '../../utils/wxAsync'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    brandId: 0,
    brandDetail: {},
    goodsList: [],
    page: 1,
    size: 20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id
    this.setData({
      // 字符串转变成数字
      brandId: id - 0
    })
    this.getBrandDetail()
    this.getGoodsList()
  },
  // 获取品牌详情数据
  async getBrandDetail() {
    let { brandId } = this.data
    const res = await request({url: '/brand/detail', data: { id: brandId }})
    console.log(res)
    this.setData({
      brandDetail: res.data.brand,
    })
  },
  // 获取品牌商品数据
  async getGoodsList() {
    let { brandId, page, size, goodsList } = this.data
    const res = await request({url: '/goods/list', data: { brandId, page, size }})
    console.log(res)
    this.setData({
      goodsList: [...goodsList, ...res.data.goodsList]
    })
  },
  // 页面触底事件
  async onReachBottom() {
    let { page, totalPages, } = this.data
    // 若有下一页数据
    if (page < totalPages) {
      page++
      this.setData({
        page
      })
      this.getGoodsList()
    } else {
      await showToast({title: '我是有底线的---', icon: 'none'})
      return
    }
  }
})